#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/1/2 0002 16:13
# @Author : Ares_Wang
# @Site :
# @File : analysis.py
# @Software: PyCharm

from src.model import *
from src.utils import *
from keras.optimizers import SGD
import numpy as np

# 分析不同等级的分类准确率
if __name__ == '__main__':
    test_filename = r'H:\wangjianlian\data\edge_imgs_test\edge1\val\20X'
    batch_size = 50

    selfModel = SelfModel()
    model = selfModel.build(input_shape = (256, 256, 3))
    # model = selfModel.build(input_shape = (256, 256, 1)) # 边缘图
    model.compile(optimizer=SGD(lr=0.00000005, momentum=0.9, nesterov=True),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model_path = r'H:\wangjianlian\project\Python\networkTest\resources\weight\temp\self_model\118model_1.h5'
    model.load_weights(model_path)

    imgs_num = 0
    y_acc = []
    for i in range(4):
        y_acc.append(0)

    data = Data()
    for _, tested_imgs, tested_labels in data.read_image(test_filename, batch_size, shuffle = False):
        imgs_num += len(tested_imgs)
        for index in range(len(tested_imgs)):
            loss_and_metrics = model.evaluate(tested_imgs[index], tested_labels[index])
            index2 = tested_labels[index].nonzero()[0][0]
            y_acc[index2] += loss_and_metrics[1]
    y_acc /= (imgs_num/4.0)

    result = Result()
    x = ['g0', 'g1', 'g2', 'g3']
    result.show_histogram(x, y_acc)

